package com.liuxinlong.modules.patent.controller;

import com.liuxinlong.annotation.LogAnnotation;
import com.liuxinlong.common.ImportResp;
import com.liuxinlong.common.UploadService;
import com.liuxinlong.constant.LogBusinessConstants;
import com.liuxinlong.constant.LogOperateConstants;
import com.liuxinlong.modules.common.controller.request.BaseDeleteReq;
import com.liuxinlong.modules.common.controller.request.QueryBaseReq;
import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.entity.PatentProposal;
import com.liuxinlong.modules.patent.controller.request.ImportPatentProposalReq;
import com.liuxinlong.modules.patent.controller.request.QueryPatentProposalReq;
import com.liuxinlong.modules.patent.controller.request.UpdatePatentProposalReq;
import com.liuxinlong.modules.patent.controller.request.UpdatePatentProposalStatusReq;
import com.liuxinlong.modules.patent.service.PatentProposalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 风险专利控制层
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2024-03-18
 */
@RestController
@RequestMapping("/patent-proposal")
public class PatentProposalController {

    @Autowired
    private PatentProposalService patentProposalService;

    @Autowired
    private UploadService uploadService;

    /**
     * 分页查询风险专利信息
     *
     * @param req 请求信息
     * @return 风险专利信息列表
     */
    @PostMapping("page")
    @LogAnnotation(value ="分页查询",business = LogBusinessConstants.PATENT_PROPOSAL,operateType = LogOperateConstants.OPERATE_QUERY)
    public BaseResp pagePatentProposalInfo(@RequestBody QueryPatentProposalReq req) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("startNum", req.getStartNum());
        queryMap.put("pageSize", req.getPageSize());
        queryMap.put("department", req.getDepartment());
        queryMap.put("projectId", req.getProjectId());
        queryMap.put("riskId", req.getRiskId());
        queryMap.put("patentName", req.getPatentName());
        queryMap.put("patentSn", req.getPatentSn());
        queryMap.put("riskLevel", req.getRiskLevel());
        queryMap.put("projectManager", req.getProjectManager());
        queryMap.put("monitor", req.getMonitor());
        List<Map<String, Object>> dataList = patentProposalService.pagePatentProposalInfo(queryMap);
        int total = patentProposalService.countPatentProposal(queryMap);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("分页查询风险专利信息成功");
        resp.setDataList(dataList);
        resp.setTotalNum(total);
        return resp;
    }

    /**
     * 新增风险专利信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("add")
    @LogAnnotation(value ="新增风险专利信息",business = LogBusinessConstants.PATENT_PROPOSAL,operateType = LogOperateConstants.OPERATE_ADD)
    public BaseResp addPatentProposalInfo(@RequestBody UpdatePatentProposalReq req) {
        PatentProposal patentProposal = new PatentProposal();
        patentProposal.setProjectId(req.getProjectId());
        patentProposal.setRiskId(req.getRiskId());
        patentProposal.setPatentName(req.getPatentName());
        patentProposal.setPatentSn(req.getPatentSn());
        patentProposal.setOpenSn(req.getOpenSn());
        patentProposal.setClassType(req.getClassType());
        patentProposal.setPatentee(req.getPatentee());
        patentProposal.setStatus(req.getStatus());
        patentProposal.setApplyDate(req.getApplyDate());
        patentProposal.setLevel(req.getLevel());
        patentProposal.setRiskType(req.getRiskType());
        patentProposal.setRightFalling(req.getRightFalling());
        patentProposal.setHandlingPlan(req.getHandlingPlan());
        patentProposal.setMonitor(req.getMonitor());
        patentProposal.setThirdReport(req.getThirdReport());
        patentProposal.setRemark(req.getRemark());
        patentProposalService.addPatentProposalInfo(patentProposal);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("新增风险专利信息成功");
        return resp;
    }

    /**
     * 修改风险专利信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("update")
    @LogAnnotation(value ="修改风险专利信息",business = LogBusinessConstants.PATENT_PROPOSAL,operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updatePatentProposalInfo(@RequestBody UpdatePatentProposalReq req) {
        PatentProposal patentProposal = new PatentProposal();
        patentProposal.setId(req.getId());
        patentProposal.setProjectId(req.getProjectId());
        patentProposal.setRiskId(req.getRiskId());
        patentProposal.setPatentName(req.getPatentName());
        patentProposal.setPatentSn(req.getPatentSn());
        patentProposal.setOpenSn(req.getOpenSn());
        patentProposal.setClassType(req.getClassType());
        patentProposal.setPatentee(req.getPatentee());
        patentProposal.setStatus(req.getStatus());
        patentProposal.setApplyDate(req.getApplyDate());
        patentProposal.setLevel(req.getLevel());
        patentProposal.setRiskType(req.getRiskType());
        patentProposal.setRightFalling(req.getRightFalling());
        patentProposal.setHandlingPlan(req.getHandlingPlan());
        patentProposal.setMonitor(req.getMonitor());
        patentProposal.setThirdReport(req.getThirdReport());
        patentProposal.setRemark(req.getRemark());
        patentProposalService.updatePatentProposalInfo(patentProposal,req.getPushUser());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改风险专利信息成功");
        return resp;
    }

    /**
     * 删除风险专利信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("delete")
    @LogAnnotation(value ="删除风险专利信息",business = LogBusinessConstants.PATENT_PROPOSAL,operateType = LogOperateConstants.OPERATE_DELETE)
    public BaseResp deletePatentProposalInfo(@RequestBody BaseDeleteReq req) {
        patentProposalService.deletePatentProposalInfo(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("删除风险专利信息成功");
        return resp;
    }

    /**
     * 修改风险专利状态信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("status")
    @LogAnnotation(value ="修改风险专利状态信息",business = LogBusinessConstants.PATENT_PROPOSAL,operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updatePatentProposalStatus(@RequestBody UpdatePatentProposalStatusReq req) {
        patentProposalService.updatePatentProposalStatus(req.getId(),req.getTargetStatus(),req.getRemark());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改风险专利状态信息成功");
        return resp;
    }

    /**
     * 查询风险专利信息
     *
     * @param req 请求信息
     * @return 风险专利信息列表
     */
    @PostMapping("batch-page")
    @LogAnnotation(value ="查询风险专利",business = LogBusinessConstants.PATENT_PROPOSAL,operateType = LogOperateConstants.OPERATE_QUERY)
    public BaseResp queryPatentProposalList(@RequestBody QueryBaseReq req) {
        List<Map<String, Object>> dataList = patentProposalService.queryPatentProposalList(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("查询风险专利信息成功");
        resp.setDataList(dataList);
        return resp;
    }

    /**
     * 导入数据预处理
     *
     * @param multipartRequest 文件
     * @return 数据
     */
    @PostMapping("import")
    @LogAnnotation(value = "导入风险专利数据", business = LogBusinessConstants.PATENT_PROPOSAL, operateType = LogOperateConstants.OPERATE_IMPORT)
    public ImportResp importPatentProposals(MultipartRequest multipartRequest) {
        Map<String, Object> map = uploadService.uploadFileLocal(multipartRequest);
        ImportResp resp = patentProposalService.importPatentProposals((String) map.get("fileRealPath"));
        return resp;
    }

    /**
     * 导入风险专利数据
     *
     * @param req 请求体
     * @return 返回导入失败数据
     */
    @PostMapping("batch-add")
    @LogAnnotation(value = "导入风险专利数据", business = LogBusinessConstants.PATENT_PROPOSAL, operateType = LogOperateConstants.OPERATE_IMPORT)
    public ImportResp batchAddPatentProposal(@RequestBody ImportPatentProposalReq req) {
        return patentProposalService.batchAddPatentProposal(req.getRiskId(),req.getDataList());
    }

    /**
     * 导出风险专利数据
     *
     * @param response response
     * @return 导出数据
     */
    @PostMapping("export")
    @LogAnnotation(value = "导出风险专利数据", business = LogBusinessConstants.PATENT_PROPOSAL, operateType = LogOperateConstants.OPERATE_EXPORT)
    public void exportPatentProposal(@RequestBody QueryPatentProposalReq req, HttpServletResponse response) {
        Map<String, Object> queryParam = new HashMap<>();
        queryParam.put("projectId", req.getProjectId());
        queryParam.put("riskId", req.getRiskId());
        queryParam.put("patentName", req.getPatentName());
        queryParam.put("patentSn", req.getPatentSn());
        queryParam.put("riskLevel", req.getRiskLevel());
        patentProposalService.exportPatentProposal(queryParam, response);
    }
}
